package com.anolesoft.yr.mbus.code.controller;

import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.code.dto.CodeMainDTO;
import com.anolesoft.yr.mbus.code.service.CodeMainService;
import com.anolesoft.yr.mbus.code.vo.CodeMainVO;
import com.anolesoft.yr.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/9/22 11:57
 */
@RestController
@RequestMapping("/api/code/codemain")
public class CodeMainController {
    @Autowired
    private CodeMainService codeMainService;

    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult create(CodeMainVO codeMainVO) {
        CodeMainDTO codeMainDTO = vo2dto(codeMainVO, true);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, codeMainService.create(codeMainDTO));
    }
    @PutMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult modify(CodeMainVO codeMainVO) {
        CodeMainDTO codeMainDTO = vo2dto(codeMainVO, false);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.MODIFY_SUCCESS_MESSAGE, codeMainService.modify(codeMainDTO));
    }

    @DeleteMapping(value = "/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable("id") String id) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, codeMainService.remove(id));
    }
    @DeleteMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@RequestParam("ids[]") List<String> ids) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, codeMainService.removeBatch(ids));
    }
    @GetMapping(value = "/get/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectOneById(@PathVariable("id") String id) {
        Optional<CodeMainDTO> codeMainDTO = this.codeMainService.selectOneById(id);
        if (codeMainDTO.isPresent()) {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, CustomBeanUtils.codeMainDTO2VO(codeMainDTO.get()));
        } else {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, null);
        }
    }
    @GetMapping(value = "/list/condition/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectList(CodeMainVO codeMainVO) {
        CodeMainDTO codeMainDTO = CodeMainDTO.builder()
                .factoryCode(codeMainVO.getFactoryCode())
                .codeName(codeMainVO.getCodeName())
                .objuid(codeMainVO.getObjuid())
                .build();
        Integer limit = codeMainVO.getRows();
        Integer curr = codeMainVO.getPage();
        PageData<List<CodeMainDTO>> pageData = this.codeMainService.selectList(codeMainDTO, limit, curr);
        List<CodeMainVO> vos = pageData.getItems().stream().map(CustomBeanUtils::codeMainDTO2VO).collect(Collectors.toList());
        PageInfo<List<CodeMainVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    private CodeMainDTO vo2dto(CodeMainVO codeMainVO, boolean newId) {
        return CodeMainDTO.builder()
                .createTime(LocalDateTime.now())
                .createUserUid(SecurityUtils.getUserId())
                .deleteFlag(Boolean.FALSE)
                .factoryCode(SecurityUtils.getFactoryCode())
                .codeName(codeMainVO.getCodeName())
                .geneClass(codeMainVO.getGeneClass())
                .codeDesc(codeMainVO.getCodeDesc())
                .modifyTime(LocalDateTime.now())
                .modifyUserUid(SecurityUtils.getUserId())
                .objuid(newId ? CommonUtils.fastUUID() : codeMainVO.getObjuid())
                .remark(codeMainVO.getRemark())
                .build();
    }
}
